﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Imaging;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace QRCodeTools {
    public static class ImageHelper {
        /// <summary>
        /// 无损压缩图片
        /// </summary>
        /// <param name="sFile">原图片地址</param>
        /// <param name="dFile">压缩后保存图片地址</param>
        /// <param name="flag">压缩质量（数字越小压缩率越高）1-100</param>
        /// <param name="size">压缩后图片的最大大小</param>
        /// <param name="sfsc">是否是第一次调用</param>
        /// <returns></returns>
        public static Image CompressImage(this Image iSource, int toWidth, int toHeight) {
            Bitmap ob = new Bitmap(toWidth, toHeight);

            using (Graphics g = Graphics.FromImage(ob)) {
                g.Clear(Color.WhiteSmoke);
                g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
                g.DrawImage(iSource, new Rectangle(0, 0, toWidth, toHeight), 0, 0, iSource.Width, iSource.Height, GraphicsUnit.Pixel);
            }
            return ob;
        }
    }
}
